using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Xml;
using SportTracksPluginFramework;
using ZoneFiveSoftware.Common.Data;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Visuals.Fitness;
using FilteredStatistics.View;
using FilteredStatistics.Controller;
using FilteredStatistics.Data;
using FilteredStatistics.Common.Controller;

namespace FilteredStatistics
{
    class PluginMain : STFrameworkEntryPoint, IPlugin
    {
        public PluginMain() : base(GUIDs.PluginMain)
        {
        }

#region STFrameworkEntryPoint Members

        protected override void Initialize()
        {
            // Make sure the different singletons are up and running to monitor
            //  logbook changes & events
            TemplateManager.Instance.GetTemplateCount();
            CustomizableCriteriasManager.Instance.Activity = null;

            // Register data track providers
            ActivityDataTrackController.Instance.RegisterDataTrackProvider(new CadenceDataTrackProvider());
            ActivityDataTrackController.Instance.RegisterDataTrackProvider(new SpeedDataTrackProvider());
            ActivityDataTrackController.Instance.RegisterDataTrackProvider(new ElevationDataTrackProvider());
            ActivityDataTrackController.Instance.RegisterDataTrackProvider(new GradeDataTrackProvider());
            ActivityDataTrackController.Instance.RegisterDataTrackProvider(new HeartRateBPMDataTrackProvider());
            ActivityDataTrackController.Instance.RegisterDataTrackProvider(new HeartRatePercentDataTrackProvider());
            ActivityDataTrackController.Instance.RegisterDataTrackProvider(new PowerDataTrackProvider());

            // Register criteria providers
            FilterCriteriaController.Instance.RegisterFilterCriteriaProvider(new FilteredStatisticsFilterCriteriasProvider());

            // Register statistics provider
            StatisticsController.Instance.RegisterProvider(FilteredStatisticsStatisticsProvider.Instance);
        }

        public override Control DonationReminderControl
        {
            get
            {
                if (m_ReminderControl == null)
                {
                    m_ReminderControl = new FilteredStatisticsDonationReminderControl();
                }

                return m_ReminderControl;
            }
        }

        public override string DonationLink
        {
            get
            {
                return "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=J3XKVCL29EEEG";
            }
        }

        public override string Name
        {
            get { return PluginView.GetLocalizedString("FilteredStatisticsText"); }
        }

        public override void ReadOptions(XmlDocument xmlDoc, XmlNamespaceManager nsmgr, XmlElement pluginNode)
        {
            List<Guid> displayedCharts = new List<Guid>();

            foreach (XmlElement child in pluginNode.ChildNodes)
            {
                if (child.Name == Constants.ChartSplitOptionsText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.ChartSplitDistance = int.Parse(child.FirstChild.Value);
                }
                else if (child.Name == Constants.FiltersSplitOptionsText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.FiltersSplitDistance = int.Parse(child.FirstChild.Value);
                }
                else if (child.Name == Constants.ChartLineColorOptionsText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.ChartLineColor = Color.FromArgb(int.Parse(child.FirstChild.Value));
                }
                else if (child.Name == Constants.ChartFillColorOptionsText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.ChartFillColor = Color.FromArgb(int.Parse(child.FirstChild.Value));
                }
                else if (child.Name == Constants.ChartSelectedColorOptionsText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.ChartSelectedColor = Color.FromArgb(int.Parse(child.FirstChild.Value));
                }
                else if (child.Name == Constants.ChartValidColorOptionsText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.ChartValidRegionColor = Color.FromArgb(int.Parse(child.FirstChild.Value));
                }
                else if (child.Name == Constants.ChartOverlayModeText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.ChartOverlayMode = bool.Parse(child.FirstChild.Value);
                }
                else if (child.Name == Constants.ChartXAxisText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.ChartXAxis = (ActivityDetailsLineChart.XAxisValue)int.Parse(child.FirstChild.Value);
                }
                else if (child.Name == Constants.ChartYAxisGUIDsText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.ChartYAxis = new Guid(child.FirstChild.Value);
                }
                else if (child.Name == Constants.CriteriaTypesColumnWidthText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.CriteriaTypesColumnWidth = int.Parse(child.FirstChild.Value);
                }
                else if (child.Name == Constants.NamedZonesColumnWidthText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.NamedZonesColumnWidth = int.Parse(child.FirstChild.Value);
                }
                else if (child.Name == Constants.OptionsCriteriaTypesColumnWidthText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.OptionsCriteriaTypesColumnWidth = int.Parse(child.FirstChild.Value);
                }
                else if (child.Name == Constants.OptionsNamedZonesColumnWidthText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.OptionsNamedZonesColumnWidth = int.Parse(child.FirstChild.Value);
                }
                else if (child.Name == Constants.ColoredColumnsText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.DrawColumnsColored = bool.Parse(child.FirstChild.Value);
                }
                else if (child.Name == Constants.ShowZonesInChartText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.ShowZonesInChart = bool.Parse(child.FirstChild.Value);
                }
                else if (child.Name == Constants.ShowTemplatesModifiedWarningText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.ShowTemplatesModifiedWarning = bool.Parse(child.FirstChild.Value);
                }
                else if (child.Name == Constants.ShowSplitsTemplatesWarningText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.ShowSplitsTemplatesWarning = bool.Parse(child.FirstChild.Value);
                }
                else if (child.Name == Constants.ResultsWindowPositionXText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.ResultsWindowPosition = new Point(int.Parse(child.FirstChild.Value), Options.Instance.ResultsWindowPosition.Y);
                }
                else if (child.Name == Constants.ResultsWindowPositionYText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.ResultsWindowPosition = new Point(Options.Instance.ResultsWindowPosition.X, int.Parse(child.FirstChild.Value));
                }
                else if (child.Name == Constants.ResultsWindowSizeWidthText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.ResultsWindowSize = new Size(int.Parse(child.FirstChild.Value), Options.Instance.ResultsWindowSize.Height);
                }
                else if (child.Name == Constants.ResultsWindowSizeHeightText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.ResultsWindowSize = new Size(Options.Instance.ResultsWindowSize.Width, int.Parse(child.FirstChild.Value));
                }
                else if (child.Name == Constants.UseMapOverlayHighlightText && child.ChildNodes.Count == 1)
                {
                    Options.Instance.UseMapOverlayHighlight = bool.Parse(child.FirstChild.Value);
                }
                else if (child.Name == Constants.ExtraDisplayChartsGUIDsText && child.ChildNodes.Count == 1)
                {
                    Guid readValue = new Guid(child.FirstChild.Value);

                    // Make sure the read value is usable
                    displayedCharts.Add(readValue);
                }
                else if (child.Name == Constants.StatisticsColumnsText)
                {
                    Options.Instance.StatisticsColumnsManager.SettingsXMLNode = child;
                }
            }

            Options.Instance.SelectedExtraCharts = displayedCharts;
        }

        public override void WriteOptions(XmlDocument xmlDoc, XmlElement pluginNode)
        {
            XmlElement child;

            child = xmlDoc.CreateElement(Constants.ChartSplitOptionsText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.ChartSplitDistance.ToString()));

            child = xmlDoc.CreateElement(Constants.FiltersSplitOptionsText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.FiltersSplitDistance.ToString()));

            child = xmlDoc.CreateElement(Constants.ChartLineColorOptionsText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.ChartLineColor.ToArgb().ToString()));

            child = xmlDoc.CreateElement(Constants.ChartFillColorOptionsText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.ChartFillColor.ToArgb().ToString()));

            child = xmlDoc.CreateElement(Constants.ChartSelectedColorOptionsText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.ChartSelectedColor.ToArgb().ToString()));

            child = xmlDoc.CreateElement(Constants.ChartValidColorOptionsText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.ChartValidRegionColor.ToArgb().ToString()));

            child = xmlDoc.CreateElement(Constants.ChartOverlayModeText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.ChartOverlayMode.ToString()));

            child = xmlDoc.CreateElement(Constants.ChartXAxisText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(((int)Options.Instance.ChartXAxis).ToString()));

            child = xmlDoc.CreateElement(Constants.ChartYAxisGUIDsText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.ChartYAxis.ToString()));

            child = xmlDoc.CreateElement(Constants.CriteriaTypesColumnWidthText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.CriteriaTypesColumnWidth.ToString()));

            child = xmlDoc.CreateElement(Constants.NamedZonesColumnWidthText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.NamedZonesColumnWidth.ToString()));

            child = xmlDoc.CreateElement(Constants.OptionsCriteriaTypesColumnWidthText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.OptionsCriteriaTypesColumnWidth.ToString()));

            child = xmlDoc.CreateElement(Constants.OptionsNamedZonesColumnWidthText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.OptionsNamedZonesColumnWidth.ToString()));

            child = xmlDoc.CreateElement(Constants.ColoredColumnsText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.DrawColumnsColored.ToString()));

            child = xmlDoc.CreateElement(Constants.ShowZonesInChartText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.ShowZonesInChart.ToString()));

            child = xmlDoc.CreateElement(Constants.ShowTemplatesModifiedWarningText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.ShowTemplatesModifiedWarning.ToString()));

            child = xmlDoc.CreateElement(Constants.ShowSplitsTemplatesWarningText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.ShowSplitsTemplatesWarning.ToString()));

            child = xmlDoc.CreateElement(Constants.UseMapOverlayHighlightText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.UseMapOverlayHighlightAbsolute.ToString()));

            child = xmlDoc.CreateElement(Constants.ResultsWindowPositionXText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.ResultsWindowPosition.X.ToString()));
            child = xmlDoc.CreateElement(Constants.ResultsWindowPositionYText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.ResultsWindowPosition.Y.ToString()));

            child = xmlDoc.CreateElement(Constants.ResultsWindowSizeWidthText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.ResultsWindowSize.Width.ToString()));
            child = xmlDoc.CreateElement(Constants.ResultsWindowSizeHeightText);
            pluginNode.AppendChild(child);
            child.AppendChild(xmlDoc.CreateTextNode(Options.Instance.ResultsWindowSize.Height.ToString()));

            foreach (Guid chartId in Options.Instance.SelectedExtraCharts)
            {
                child = xmlDoc.CreateElement(Constants.ExtraDisplayChartsGUIDsText);
                pluginNode.AppendChild(child);
                child.AppendChild(xmlDoc.CreateTextNode(chartId.ToString()));
            }

            Options.Instance.StatisticsColumnsManager.SaveToSettings(xmlDoc, pluginNode, Constants.StatisticsColumnsText);
        }

#endregion

        private FilteredStatisticsDonationReminderControl m_ReminderControl = null;
    }
}
